//表单验证
$(function(){
	function savePay(){
		//获取form的值
		var ts = $('#ts').val();
		var orderId = $('#orderId').val();
		var payMethod = $('#payMethod').val();
		var payAccount = $('#payAccount').val();
		var noPayAmount = $('#noPayAmount').val();
		var payMoney = $('#payMoney').val();
		var remark = $('#remark').val();
		
		var buyerCode = $('#buyerCode').val();
		var buyerName = $('#buyerName').val();
		
		var payPicImage = $('#payPicImage').attr("src");
		
		if(orderId == null || orderId == ""){
			layer.msg("订单id为空，请刷新后重试!",{icon:2,time:2000});
			return;
		}
		if(payMethod == null || payMethod == ""){
			layer.msg("请选择支付方式!",{icon:2,time:2000});
			return;
		}
		if(payAccount == null || payAccount == ""){
			layer.msg("请输入支付账号!",{icon:2,time:2000});
			return;
		}
		if(payMoney == null || payMoney == ""){
			layer.msg("请输入支付金额!",{icon:2,time:2000});
			return;
		}
//		if(payMoney > noPayAmount){
//			layer.msg("当前支付金额不能大于该订单待支付金额!",{icon:2,time:2000});
//			return;
//		}
		if(remark != null && remark.length > 512){
			layer.msg("支付备注不能超过512位!",{icon:2,time:2000});
			return;
		}
		
		var pay = {};
		pay["ts"] = ts;
		pay["orderId"] = orderId;
		pay["payMethod"] = payMethod;
		pay["payAccount"] = payAccount;
		pay["payMoney"] = payMoney;
		pay["remark"] = remark;
		pay["payPic"] = payPicImage;
		
		pay["buyerCode"] = buyerCode;
		pay["buyerName"] = buyerName;
		
		$.ajax({
			url:baselocation+'/pay',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(pay),
			success:function(result){
				$('#savePayBtn').attr("disabled",false);
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					layer.msg(result.message,{icon:1,time:2000});
					setTimeout(goToOrderDetails, 2000);
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	}
	//跳转list页
	function goToOrderDetails(){
		var orderId = $('#orderId').val();
		window.location.href=baselocation+'/pay/'+orderId+'/list';
	}
	$('#savePayBtn').click(function () {
		$('#savePayBtn').attr("disabled",true);
		savePay();
	})
});
function payConfirm(status){
	var ts = $('#ts').val();
	var id = $('#id').val();
	var orderId = $('#orderId').val();
	var payMoney = $('#payMoney').val();
	var pay = {};
	pay["ts"] = ts;
	pay["id"] = id;
	pay["status"] = status;
	pay["orderId"] = orderId;
	pay["payMoney"] = payMoney;
	$.ajax({
		url:baselocation+'/pay/pay-status-update',
		contentType:'application/json',
		type:'post',
		dataType:'json',
		data: JSON.stringify(pay),
		success:function(result){
			if(result.success==false){
				layer.msg(result.message,{icon:2,time:2000});
			}else{
				layer.msg(result.message,{icon:1,time:2000});
				if(status == 0){
					$('#payStatusName_'+id).html("支付中");
				}else{
					$('#payStatusName_'+id).html("支付成功");
				}
			}
		},
		error:function(result){
			alert("系统异常,请联系管理员！");
		}
	});
}